package com.turtle3d.gui;

/*                         \\|//
                           {o o}
~~~~~~~~~~~~~~~~~~~~~~~oOOo~\-/~oOOo~~~~~~~~~~~~~~~~~~~~~~~~
|               DESGRAFF enr, DECOY PROJECT                |
|                 Copyright David Deschenes                    |
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Nom:  CFrameWork.java
Date: 14 sept 2002, update: 26 May 2003
Auteur: David Deschenes
Desc: FrameWork for Java3D projects
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/


import java.awt.BorderLayout;

import javax.media.j3d.AmbientLight;
import javax.media.j3d.BoundingSphere;
import javax.media.j3d.BranchGroup;
import javax.media.j3d.Canvas3D;
import javax.media.j3d.DirectionalLight;
import javax.media.j3d.GeometryArray;
import javax.media.j3d.LineArray;
import javax.media.j3d.Shape3D;
import javax.media.j3d.TransformGroup;
import javax.swing.JApplet;
import javax.vecmath.Color3f;
import javax.vecmath.Point3d;
import javax.vecmath.Point3f;
import javax.vecmath.Vector3f;


import com.sun.j3d.utils.applet.MainFrame;
import com.sun.j3d.utils.behaviors.keyboard.KeyNavigatorBehavior;
import com.sun.j3d.utils.behaviors.mouse.MouseBehavior;
import com.sun.j3d.utils.behaviors.mouse.MouseRotate;
import com.sun.j3d.utils.behaviors.mouse.MouseTranslate;
import com.sun.j3d.utils.behaviors.mouse.MouseZoom;
import com.sun.j3d.utils.universe.SimpleUniverse;

public abstract class CFrameWork extends JApplet {
  //Global vars
    public SimpleUniverse oSimpleUniverse_1 = null;
  public BranchGroup oBranchGroup_1  = null;

/**Fonction : createSceneGraph ()
  params: void
  retour: void
  desc: creating the SceneGraph (content)*/
  public abstract BranchGroup createSceneGraph();

/**Fonction : CFrameWork()
   params: void
   retour: void
   desc: constructor */
  public CFrameWork( ){

    getContentPane().setLayout(new BorderLayout());  //Null borders, if not gray screen
    //1-Object Canvas3d
    Canvas3D oCanvas3D_1 = new Canvas3D(SimpleUniverse.getPreferredConfiguration ());
    getContentPane().add( "Center", oCanvas3D_1); //Add canvas to container "Applet"

    //2-Make View SimpleUniverse(Canvas3d canvas3d)
    oSimpleUniverse_1 = new SimpleUniverse(oCanvas3D_1);
    //  Backup camera a bit (2.41m)
    oSimpleUniverse_1.getViewingPlatform().setNominalViewingTransform();

    //3-ini Content branch
    BranchGroup oBranchGroup_1 = createSceneGraph( );
    oSimpleUniverse_1.addBranchGraph( oBranchGroup_1 );
  }

/**Fonction : destroy()
  params: void
  retour: void
  desc: cleanup */
  public void destroy(){

    oSimpleUniverse_1.removeAllLocales();
  }

/**Fonction : addKeyMouseSupport()
  params: void
  retour: void
  desc: allow transforms to the root node with the mouse*/
  public void addKeyMouseSupport(){

    BoundingSphere oBoundingSphere_1 = null;
      TransformGroup  oTransformGroup_1 = null;
        oBoundingSphere_1  = new BoundingSphere(new Point3d(), 1000.0);
        oTransformGroup_1 = oSimpleUniverse_1.getViewingPlatform().getViewPlatformTransform();


    KeyNavigatorBehavior oKeyNavigatorBehavior_1 = new KeyNavigatorBehavior(oTransformGroup_1);
    oKeyNavigatorBehavior_1.setSchedulingBounds(oBoundingSphere_1);
    oBranchGroup_1.addChild(oKeyNavigatorBehavior_1);

    MouseRotate oMouseRotate_1 = new MouseRotate(MouseBehavior.INVERT_INPUT);
        oMouseRotate_1.setTransformGroup(oTransformGroup_1);
        oMouseRotate_1.setSchedulingBounds(oBoundingSphere_1);
        oBranchGroup_1.addChild(oMouseRotate_1);

        MouseZoom oMouseZoom_1 = new MouseZoom(MouseBehavior.INVERT_INPUT);
        oMouseZoom_1.setTransformGroup(oTransformGroup_1);
        oMouseZoom_1.setSchedulingBounds(oBoundingSphere_1);
        oBranchGroup_1.addChild(oMouseZoom_1);

        MouseTranslate oMouseTranslate_1 = new MouseTranslate(MouseBehavior.INVERT_INPUT);
        oMouseTranslate_1.setTransformGroup(oTransformGroup_1);
        oMouseTranslate_1.setSchedulingBounds(oBoundingSphere_1);
        oBranchGroup_1.addChild(oMouseTranslate_1);
  }

  public AmbientLight addAmbientLight(Color3f color){
    BoundingSphere bounds = new BoundingSphere(new Point3d(0.0, 0.0, 0.0), 100.0);
    Color3f alColour = new Color3f(0.4f, 0.4f, 0.4f);
    AmbientLight aLgt = new AmbientLight(alColour);
    aLgt.setInfluencingBounds(bounds);
    return aLgt;
  }


   public DirectionalLight addDirectionalLight(Vector3f direction, Color3f color){

      // Creates a bounding sphere for the lights
         BoundingSphere bounds = new BoundingSphere();
         bounds.setRadius(1000d);

      // Then create a directional light with the given
      // direction and color
         DirectionalLight lightD = new DirectionalLight(color, direction);
         lightD.setInfluencingBounds(bounds);
     return lightD;
     }

	/**
	 * Fonction : grid params: void retour: Shape3D desc: convert to app with
	 * mainframe()
	 */
     public Shape3D grid(){

        LineArray oLineArray_1 =
  new LineArray(44, GeometryArray.COORDINATES | GeometryArray.COLOR_3);


    for(int x = 0; x < 22; x+=2){

        oLineArray_1.setCoordinate( x, new Point3f( -10.0f,  -0.5f,  x-10 ));
            oLineArray_1.setCoordinate( x+1, new Point3f(  10.0f,  -0.5f,  x-10 ));
    }

    for(int y = 22; y < 44; y+=2){

        oLineArray_1.setCoordinate( y, new Point3f( y-32,  -0.5f, -10.0f ));
            oLineArray_1.setCoordinate( y+1, new Point3f( y-32 , -0.5f,  10.0f ));
    }

    Color3f c = new Color3f(1.0f, 1.0f, 0.0f);
        for(int i = 0; i < 44; i++){
      oLineArray_1.setColor( i, c);
    }

        return new Shape3D(oLineArray_1);
    }

/**Fonction : main(String[] args)
   params: string array
   retour: void
   desc:  convert to app with mainframe()*/
    public static void main(String[] args){

    new MainFrame(new CProject(), 778, 431);
    }

}
